package com.cskaoyan.service;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.vo.MarketAdminVO;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/6 19:53
 * @Description:
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public CommonData<MarketAdminVO> listAdmin(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        example.setOrderByClause(sort + " " + order);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        List<MarketAdminVO> marketAdminVOList = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {
            MarketAdminVO marketAdminVO = new MarketAdminVO();
            marketAdminVO.setId(marketAdmin.getId());
            marketAdminVO.setAvatar(marketAdmin.getAvatar());
            marketAdminVO.setUsername(marketAdmin.getUsername());
            marketAdminVO.setRoleIds(marketAdmin.getRoleIds());
            marketAdminVOList.add(marketAdminVO);
        }
        PageInfo pageInfo = new PageInfo(marketAdminVOList);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAdmin createAdmin(MarketAdmin marketAdmin) {
        marketAdmin.setAddTime(new Date());
        marketAdmin.setUpdateTime(new Date());
        marketAdminMapper.insertSelective(marketAdmin);
        MarketAdmin marketAdmin1 = marketAdminMapper.selectByPrimaryKey(marketAdmin.getId());
        return marketAdmin1;
    }

    @Override
    public MarketAdmin updateAdmin(MarketAdmin marketAdmin) {
        marketAdmin.setUpdateTime(new Date());
        int i = marketAdminMapper.updateByPrimaryKeySelective(marketAdmin);
        MarketAdmin marketAdmin1 = marketAdminMapper.selectByPrimaryKey(marketAdmin.getId());
        return marketAdmin1;
    }

    @Override
    public Integer deleteAdmin(MarketAdmin marketAdmin) {
        Integer rows = marketAdminMapper.deleteByPrimaryKey(marketAdmin.getId());
        return rows;
    }
}
